TryFindImage(path){
	global FoundX
	global FoundY
	finded=0
        CoordMode Pixel, Screen
	while(finded=0)	{
		ImageSearch, FoundX, FoundY, 0,0, A_ScreenWidth, A_ScreenHeight, %path%
		if(ErrorLevel != 2 && ErrorLevel != 1){
			return 1
		}
	}
}


GetLastFindedImageX(){
	global FoundX
	return FoundX
}

GetLastFindedImageY(){
	global FoundY
	return FoundY
}

FindPictureAndMove(arg1, path, clickX, clickY, arg2){
	CoordMode Mouse, Screen
	res:=TryFindImage(path)
	if(res==1){
		x:=GetLastFindedImageX()+clickX
		y:=GetLastFindedImageY()+clickY
		MouseMove x,y
	}
}

WaitForImage(arg1, path, clickX, clickY, arg2){
	CoordMode Pixel, Screen
        global FoundX
	global FoundY
	finded=0
	while(finded=0)	{
		ImageSearch, FoundX, FoundY, 0,0, A_ScreenWidth, A_ScreenHeight, %path%
		if(ErrorLevel != 2 && ErrorLevel != 1){
			;MsgBox, founded
			return 1
		}
	}
}

IfFindImage(arg1, path, clickX, clickY, arg2){
        CoordMode Pixel, Screen	
        ImageSearch, FoundX, FoundY, 0,0, A_ScreenWidth, A_ScreenHeight, %path%
	if( ErrorLevel != 2 && ErrorLevel != 1)
		return 1
	else
		return 0
}

FindPictureAndClick(arg1, path, clickX, clickY, arg2){
	CoordMode Mouse, Screen
	res:=TryFindImage(path)
	if(res==1){
		tx:=GetLastFindedImageX()
		ty:=GetLastFindedImageY()
		x:=tx+clickX
		y:=ty+clickY
		MouseMove x,y
		Sleep, 10  
		Click x, y
		Sleep, 10
	}
}